package com.woniu.consumer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.model.Message;
import com.woniu.service.MessageService;
import com.woniu.utils.RedisKeyUtil;
import com.woniu.utils.RedisUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 新增到消息数据到 数据库 的消费者
 */
@Component
@RabbitListener(queues = "message_queue")
public class MessageConsumer {
    @Resource
    private MessageService messageService;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private RedisUtil redisUtil;
    @RabbitHandler
    public void insertMessage(HashMap<String, String> map) throws JsonProcessingException {
        String userId = map.get("userId");
        String roomNum = map.get("roomNum");
        if (redisUtil.exist(RedisKeyUtil.userIdStartTalkScore(userId))) {
            //取出用户开始聊天时的score
            String score = redisUtil.get(RedisKeyUtil.userIdStartTalkScore(userId));
            //取出 大于 score 的聊天数据(按照从小到大的方式排列)
            List<String> list = redisUtil.
                    zrangeByScore(RedisKeyUtil.talkRoomNum(roomNum), Double.parseDouble(score)+1, Double.MAX_VALUE)
                    .stream().collect(Collectors.toList());
            if (list!=null&&list.size()>0){
                List<Message> messages = new ArrayList<>();
                for (String s : list) {
                    //{"roomNum":"1800348402249240576","message":"111","accountId":1,"userImg":null,"createTime":"1718071553030"}
                    Map<String,Object> message = objectMapper.readValue(s, Map.class);
                    Message message1 = new Message();
                    message1.setRoomNum(message.get("roomNum").toString());
                    message1.setMessage(message.get("message").toString());
                    message1.setAccountId( (int)(message.get("accountId")));
                    message1.setUserImg(message.get("userImg")==null?null:message.get("userImg").toString());
                    Object createTimeObj = message.get("createTime");
                    if (createTimeObj != null) {
                        long timestamp = Long.parseLong(createTimeObj.toString());
                        LocalDateTime createTime = LocalDateTimeUtil.of(timestamp);
                        message1.setCreateTime(createTime.toString());
                        System.out.println("getCreateTime");
                        System.out.println(message1.getCreateTime());
                    }
                    messages.add(message1);
                }
                System.out.println("消息队列执行数据库新增操作messages");
                System.out.println(messages);
                messageService.saveBatch(messages);
            }
        }

    }
}
